import { 
  createRouter, 
  createWebHistory, 
  type RouteRecordRaw,
  type Router,
  type RouteLocationNormalized,
  type NavigationGuardNext
} from 'vue-router'
import type { Component } from 'vue'
import Home from '../pages/home/Home.vue'
import { useUserStore } from '@/stores/user'

// 扩展 RouteRecordRaw 类型以包含 meta 字段
interface CustomRouteRecordRaw extends Omit<RouteRecordRaw, 'children'> {
  meta?: {
    requiresAuth?: boolean
  }
  children?: CustomRouteRecordRaw[]
}

const routes: CustomRouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: Home as Component
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../pages/auth/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../pages/auth/Register.vue')
  },
  {
    path: '/destination',
    name: 'Destination',
    component: () => import('../pages/destination/Destination.vue')
  },
  {
    path: '/group-buy',
    name: 'GroupBuy',
    component: () => import('../pages/group-buy/GroupBuy.vue')
  },
  {
    path: '/guide',
    name: 'Guide',
    component: () => import('../pages/guide/Guide.vue')
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../pages/profile/Profile.vue')
  },
  {
    path: '/destination/:id',
    name: 'DestinationDetail',
    component: () => import('../pages/destination/DestinationDetail.vue')
  },
  {
    path: '/guide/:id',
    name: 'GuideDetail',
    component: () => import('../pages/guide/GuideDetail.vue')
  },
  {
    path: '/group-buy/:id',
    name: 'GroupBuyDetail',
    component: () => import('../pages/group-buy/GroupBuyDetail.vue')
  },
  {
    path: '/my-services',
    name: 'MyServices',
    component: () => import('../pages/service/MyServices.vue'),
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '',
        redirect: '/my-services/account'
      },
      {
        path: 'account',
        name: 'MyAccount',
        component: () => import('../pages/service/MyAccount.vue')
      },
      {
        path: 'tours',
        name: 'MyTours',
        component: () => import('../pages/service/MyTours.vue')
      },
      {
        path: 'posts',
        name: 'MyPosts',
        component: () => import('../pages/service/MyPosts.vue')
      },
      {
        path: 'comments',
        name: 'MyComments',
        component: () => import('../pages/service/MyComments.vue')
      },
      {
        path: 'weather',
        name: 'MyWeather',
        component: () => import('../pages/service/MyWeather.vue')
      }
    ]
  },
  {
    path: '/developing',
    name: 'UnderDevelopment',
    component: () => import('../pages/common/UnderDevelopment.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: routes as RouteRecordRaw[]
})

// 导航守卫
router.beforeEach((
  to: RouteLocationNormalized,
  from: RouteLocationNormalized,
  next: NavigationGuardNext
) => {
  const userStore = useUserStore()
  
  if (to.meta?.requiresAuth && !userStore.isLoggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router 